<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>Vue CLI3之pages 构建多页应用 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="Vue,">
  

  <meta name="description" content="一、概念 首先我们可以把多页应用理解为由多个单页构成的应用，而何谓多个单页呢？其实你可以把一个单页看成是一个 html 文件，那么多个单页便是多个 html 文件，多页应用便是由多个 html 组成的应用，如下图所示    既然多页应用拥有多个 html，那么同样其应该拥有多个独立的入口文件、组件、路由、vuex 等。没错，说简单一点就是多页应用的每个单页都可以拥有单页应用 src 目录下的文件及">
<meta name="keywords" content="Vue">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue CLI3之pages 构建多页应用">
<meta property="og:url" content="http://blog.poetries.top/2019/06/01/vue-muti-page-config/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、概念 首先我们可以把多页应用理解为由多个单页构成的应用，而何谓多个单页呢？其实你可以把一个单页看成是一个 html 文件，那么多个单页便是多个 html 文件，多页应用便是由多个 html 组成的应用，如下图所示    既然多页应用拥有多个 html，那么同样其应该拥有多个独立的入口文件、组件、路由、vuex 等。没错，说简单一点就是多页应用的每个单页都可以拥有单页应用 src 目录下的文件及">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2018/7/30/164eb40e7ae7fb85">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2018/10/30/166c0c419bc53201">
<meta property="og:image" content="https://user-gold-cdn.xitu.io/2018/10/30/166c0c888939ac36">
<meta property="og:updated_time" content="2020-08-15T04:25:31.938Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue CLI3之pages 构建多页应用">
<meta name="twitter:description" content="一、概念 首先我们可以把多页应用理解为由多个单页构成的应用，而何谓多个单页呢？其实你可以把一个单页看成是一个 html 文件，那么多个单页便是多个 html 文件，多页应用便是由多个 html 组成的应用，如下图所示    既然多页应用拥有多个 html，那么同样其应该拥有多个独立的入口文件、组件、路由、vuex 等。没错，说简单一点就是多页应用的每个单页都可以拥有单页应用 src 目录下的文件及">
<meta name="twitter:image" content="https://user-gold-cdn.xitu.io/2018/7/30/164eb40e7ae7fb85">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、概念"><span class="toc-text">一、概念</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、多入口"><span class="toc-text">二、多入口</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、多模板"><span class="toc-text">三、多模板</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四、使用-pages-配置"><span class="toc-text">四、使用 pages 配置</span></a></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-vue-muti-page-config" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">Vue CLI3之pages 构建多页应用</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2019.06.02</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 2.3k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 9分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、概念"><a href="#一、概念" class="headerlink" title="一、概念"></a>一、概念</h2><blockquote>
<p>首先我们可以把多页应用理解为由多个单页构成的应用，而何谓多个单页呢？其实你可以把一个单页看成是一个 <code>html</code> 文件，那么多个单页便是多个 <code>html</code> 文件，多页应用便是由多个 <code>html</code> 组成的应用，如下图所示</p>
</blockquote>
<p><img src="https://user-gold-cdn.xitu.io/2018/7/30/164eb40e7ae7fb85" alt></p>
<blockquote>
<p>既然多页应用拥有多个 <code>html</code>，那么同样其应该拥有多个独立的入口文件、组件、路由、<code>vuex</code> 等。没错，说简单一点就是多页应用的每个单页都可以拥有单页应用 <code>src</code> 目录下的文件及功能，我们来看一下一个基础多页应用的目录结构</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">├── node_modules               <span class="comment"># 项目依赖包目录</span></span><br><span class="line">├── build                      <span class="comment"># 项目 webpack 功能目录</span></span><br><span class="line">├── config                     <span class="comment"># 项目配置项文件夹</span></span><br><span class="line">├── src                        <span class="comment"># 前端资源目录</span></span><br><span class="line">│   ├── images                 <span class="comment"># 图片目录</span></span><br><span class="line">│   ├── components             <span class="comment"># 公共组件目录</span></span><br><span class="line">│   ├── pages                  <span class="comment"># 页面目录</span></span><br><span class="line">│   │   ├── page1              <span class="comment"># page1 目录</span></span><br><span class="line">│   │   │   ├── components     <span class="comment"># page1 组件目录</span></span><br><span class="line">│   │   │   ├── router         <span class="comment"># page1 路由目录</span></span><br><span class="line">│   │   │   ├── views          <span class="comment"># page1 页面目录</span></span><br><span class="line">│   │   │   ├── page1.html     <span class="comment"># page1 html 模板</span></span><br><span class="line">│   │   │   ├── page1.vue      <span class="comment"># page1 vue 配置文件</span></span><br><span class="line">│   │   │   └── page1.js       <span class="comment"># page1 入口文件</span></span><br><span class="line">│   │   ├── page2              <span class="comment"># page2 目录</span></span><br><span class="line">│   │   └── index              <span class="comment"># index 目录</span></span><br><span class="line">│   ├── common                 <span class="comment"># 公共方法目录</span></span><br><span class="line">│   └── store                  <span class="comment"># 状态管理 store 目录</span></span><br><span class="line">├── .gitignore                 <span class="comment"># git 忽略文件</span></span><br><span class="line">├── .env                       <span class="comment"># 全局环境配置文件</span></span><br><span class="line">├── .env.dev                   <span class="comment"># 开发环境配置文件</span></span><br><span class="line">├── .postcssrc.js              <span class="comment"># postcss 配置文件</span></span><br><span class="line">├── babel.config.js            <span class="comment"># babel 配置文件</span></span><br><span class="line">├── package.json               <span class="comment"># 包管理文件</span></span><br><span class="line">├── vue.config.js              <span class="comment"># CLI 配置文件</span></span><br><span class="line">└── yarn.lock                  <span class="comment"># yarn 依赖信息文件</span></span><br></pre></td></tr></table></figure>
<h2 id="二、多入口"><a href="#二、多入口" class="headerlink" title="二、多入口"></a>二、多入口</h2><blockquote>
<p>在单页应用中，我们的入口文件只有一个，<code>CLI</code> 默认配置的是 <code>main.js</code>，但是到了多页应用，我们的入口文件便包含了 <code>page1.js</code>、<code>page2.js</code>、<code>index.js</code>等，数量取决于 <code>pages</code> 文件夹下目录的个数，这时候为了项目的可拓展性，我们需要自动计算入口文件的数量并解析路径配置到 <code>webpack</code> 中的 <code>entry</code> 属性上，如：</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    entry: &#123;</span><br><span class="line">        page1: <span class="string">'/xxx/pages/page1/page1.js'</span>,</span><br><span class="line">        page2: <span class="string">'/xxx/pages/page2/page2.js'</span>,</span><br><span class="line">        index: <span class="string">'/xxx/pages/index/index.js'</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>那么我们如何读取并解析这样的路径呢，这里就需要使用工具和函数来解决了。我们可以在根目录新建 <code>build</code> 文件夹存放 <code>utils.js</code> 这样共用的 <code>webpack</code> 功能性文件，并加入多入口读取解析方法</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* utils.js */</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// glob 是 webpack 安装时依赖的一个第三方模块，该模块允许你使用 * 等符号,</span></span><br><span class="line"><span class="comment">// 例如 lib/*.js 就是获取 lib 文件夹下的所有 js 后缀名的文件</span></span><br><span class="line"><span class="keyword">const</span> glob = <span class="built_in">require</span>(<span class="string">'glob'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 取得相应的页面路径，因为之前的配置，所以是 src 文件夹下的 pages 文件夹</span></span><br><span class="line"><span class="keyword">const</span> PAGE_PATH = path.resolve(__dirname, <span class="string">'../src/pages'</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">/* </span></span><br><span class="line"><span class="comment">* 多入口配置</span></span><br><span class="line"><span class="comment">* 通过 glob 模块读取 pages 文件夹下的所有对应文件夹下的 js * 后缀文件，如果该文件存在</span></span><br><span class="line"><span class="comment">* 那么就作为入口处理</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line">exports.getEntries = <span class="function"><span class="params">()</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> entryFiles = glob.sync(PAGE_PATH + <span class="string">'/*/*.js'</span>) <span class="comment">// 同步读取所有入口文件</span></span><br><span class="line">    <span class="keyword">let</span> map = &#123;&#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">// 遍历所有入口文件</span></span><br><span class="line">    entryFiles.forEach(<span class="function"><span class="params">filePath</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="comment">// 获取文件名</span></span><br><span class="line">        <span class="keyword">let</span> filename = filePath.substring(filePath.lastIndexOf(<span class="string">'\/'</span>) + <span class="number">1</span>, filePath.lastIndexOf(<span class="string">'.'</span>))</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 以键值对的形式存储</span></span><br><span class="line">        map[filename] = filePath </span><br><span class="line">    &#125;)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> map</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://user-gold-cdn.xitu.io/2018/10/30/166c0c419bc53201" alt></p>
<blockquote>
<p>读取并存储完毕后，我们得到了一个入口文件的对象集合，这个对象我们便可以将其设置到 <code>webpack</code> 的 <code>entry</code> 属性上，这里我们需要修改 <code>vue.config.js</code> 的配置来间接修改 <code>webpack</code> 的值</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* vue.config.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> utils = <span class="built_in">require</span>(<span class="string">'./build/utils'</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    configureWebpack: <span class="function"><span class="params">config</span> =&gt;</span> &#123;</span><br><span class="line">        config.entry = utils.getEntries()</span><br><span class="line">    &#125;,</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样我们多入口的设置便完成了，当然这并不是 CLI 所希望的操作，后面我们会进行改进。</p>
</blockquote>
<h2 id="三、多模板"><a href="#三、多模板" class="headerlink" title="三、多模板"></a>三、多模板</h2><blockquote>
<p>相对于多入口来说，多模板的配置也是大同小异，这里所说的模板便是每个 <code>page</code> 下的 <code>html</code> 模板文件，而模板文件的作用主要用于 <code>webpack</code> 中 <code>html-webpack-plugin</code> 插件的配置，其会根据模板文件生产一个编译后的 <code>html</code> 文件并自动加入携带 hash 的脚本和样式，基本配置如下</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* webpack 配置文件 */</span></span><br><span class="line"><span class="keyword">const</span> HtmlWebpackPlugin = <span class="built_in">require</span>(<span class="string">'html-webpack-plugin'</span>) <span class="comment">// 安装并引用插件</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    plugins: [</span><br><span class="line">        <span class="keyword">new</span> HtmlWebpackPlugin(&#123;</span><br><span class="line">            title: <span class="string">'My Page'</span>, <span class="comment">// 生成 html 中的 title</span></span><br><span class="line">            filename: <span class="string">'demo.html'</span>, <span class="comment">// 生成 html 的文件名</span></span><br><span class="line">            template: <span class="string">'xxx/xxx/demo.html'</span>, <span class="comment">// 模板路径</span></span><br><span class="line">            chunks: [<span class="string">'manifest'</span>, <span class="string">'vendor'</span>, <span class="string">'demo'</span>], <span class="comment">// 所要包含的模块</span></span><br><span class="line">            inject: <span class="literal">true</span>, <span class="comment">// 是否注入资源</span></span><br><span class="line">        &#125;)</span><br><span class="line">    ]</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>以上是单模板的配置，那么如果是多模板只要继续往 <code>plugins</code> 数组中添加 <code>HtmlWebpackPlugin</code> 即可，但是为了和多入口一样能够灵活的获取 <code>pages</code> 目录下所有模板文件并进行配置，我们可以在 <code>utils.js</code> 中添加多模板的读取解析方法</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* utils.js */</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 多页面输出配置</span></span><br><span class="line"><span class="comment">// 与上面的多页面入口配置相同，读取 page 文件夹下的对应的 html 后缀文件，然后放入数组中</span></span><br><span class="line">exports.htmlPlugin = <span class="function"><span class="params">configs</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> entryHtml = glob.sync(PAGE_PATH + <span class="string">'/*/*.html'</span>)</span><br><span class="line">    <span class="keyword">let</span> arr = []</span><br><span class="line">    </span><br><span class="line">    entryHtml.forEach(<span class="function"><span class="params">filePath</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> filename = filePath.substring(filePath.lastIndexOf(<span class="string">'\/'</span>) + <span class="number">1</span>, filePath.lastIndexOf(<span class="string">'.'</span>))</span><br><span class="line">        <span class="keyword">let</span> conf = &#123;</span><br><span class="line">            template: filePath, <span class="comment">// 模板路径</span></span><br><span class="line">            filename: filename + <span class="string">'.html'</span>, <span class="comment">// 生成 html 的文件名</span></span><br><span class="line">            chunks: [<span class="string">'manifest'</span>, <span class="string">'vendor'</span>,  filename],</span><br><span class="line">            inject: <span class="literal">true</span>,</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 如果有自定义配置可以进行 merge</span></span><br><span class="line">        <span class="keyword">if</span> (configs) &#123;</span><br><span class="line">            conf = merge(conf, configs)</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 针对生产环境配置</span></span><br><span class="line">        <span class="keyword">if</span> (process.env.NODE_ENV === <span class="string">'production'</span>) &#123;</span><br><span class="line">            conf = merge(conf, &#123;</span><br><span class="line">                minify: &#123;</span><br><span class="line">                    removeComments: <span class="literal">true</span>, <span class="comment">// 删除 html 中的注释代码</span></span><br><span class="line">                    collapseWhitespace: <span class="literal">true</span>, <span class="comment">// 删除 html 中的空白符</span></span><br><span class="line">                    <span class="comment">// removeAttributeQuotes: true // 删除 html 元素中属性的引号</span></span><br><span class="line">                &#125;,</span><br><span class="line">                chunksSortMode: <span class="string">'manual'</span> <span class="comment">// 按 manual 的顺序引入</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line">        </span><br><span class="line">        arr.push(<span class="keyword">new</span> HtmlWebpackPlugin(conf))</span><br><span class="line">    &#125;)</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> arr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p><img src="https://user-gold-cdn.xitu.io/2018/10/30/166c0c888939ac36" alt></p>
<ul>
<li>以上我们仍然是使用 <code>glob</code> 读取所有模板文件，然后将其遍历并设置每个模板的 <code>config</code>，同时针对一些自定义配置和生产环境的配置进行了 <code>merge</code> 处理，其中自定义配置的功能我会在下节进行介绍，这里介绍一下生产环境下 <code>minify</code> 配置的作用：将 <code>html-minifier</code> 的选项作为对象来缩小输出。</li>
<li><code>html-minifier</code> 是一款用于缩小 <code>html</code> 文件大小的工具，其有很多配置项功能，包括上述所列举的常用的删除注释、空白、引号等。</li>
<li>当我们编写完了多模板的方法后，我们同样可以在 <code>vue.config.js</code> 中进行配置，与多入口不同的是我们在 <code>configureWebpack</code> 中不能直接替换 <code>plugins</code> 的值，因为它还包含了其他插件</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* vue.config.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> utils = <span class="built_in">require</span>(<span class="string">'./build/utils'</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    configureWebpack: <span class="function"><span class="params">config</span> =&gt;</span> &#123;</span><br><span class="line">        config.entry = utils.getEntries() <span class="comment">// 直接覆盖 entry 配置</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 使用 return 一个对象会通过 webpack-merge 进行合并，plugins 不会置空</span></span><br><span class="line">        <span class="keyword">return</span> &#123;</span><br><span class="line">            plugins: [...utils.htmlPlugin()]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>如此我们多页应用的多入口和多模板的配置就完成了，这时候我们运行命令 <code>yarn build</code> 后你会发现 <code>dist</code> 目录下生成了 <code>3</code> 个 <code>html</code> 文件，分别是 <code>index.html</code>、<code>page1.html</code> 和 <code>page2.html</code></p>
</blockquote>
<h2 id="四、使用-pages-配置"><a href="#四、使用-pages-配置" class="headerlink" title="四、使用 pages 配置"></a>四、使用 pages 配置</h2><blockquote>
<p>其实，在 <code>vue.config.js</code> 中，我们还有一个配置没有使用，便是 <code>pages</code>。<code>pages</code> 对象允许我们为应用配置多个入口及模板，这就为我们的多页应用提供了开放的配置入口。官方示例代码如下</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* vue.config.js */</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    pages: &#123;</span><br><span class="line">        index: &#123;</span><br><span class="line">            <span class="comment">// page 的入口</span></span><br><span class="line">            entry: <span class="string">'src/index/main.js'</span>,</span><br><span class="line">            <span class="comment">// 模板来源</span></span><br><span class="line">            template: <span class="string">'public/index.html'</span>,</span><br><span class="line">            <span class="comment">// 在 dist/index.html 的输出</span></span><br><span class="line">            filename: <span class="string">'index.html'</span>,</span><br><span class="line">            <span class="comment">// 当使用 title 选项时，</span></span><br><span class="line">            <span class="comment">// template 中的 title 标签需要是 &lt;title&gt;&lt;%= htmlWebpackPlugin.options.title %&gt;&lt;/title&gt;</span></span><br><span class="line">            title: <span class="string">'Index Page'</span>,</span><br><span class="line">            <span class="comment">// 在这个页面中包含的块，默认情况下会包含</span></span><br><span class="line">            <span class="comment">// 提取出来的通用 chunk 和 vendor chunk。</span></span><br><span class="line">            chunks: [<span class="string">'chunk-vendors'</span>, <span class="string">'chunk-common'</span>, <span class="string">'index'</span>]</span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="comment">// 当使用只有入口的字符串格式时，</span></span><br><span class="line">        <span class="comment">// 模板会被推导为 `public/subpage.html`</span></span><br><span class="line">        <span class="comment">// 并且如果找不到的话，就回退到 `public/index.html`。</span></span><br><span class="line">        <span class="comment">// 输出文件名会被推导为 `subpage.html`。</span></span><br><span class="line">        subpage: <span class="string">'src/subpage/main.js'</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>我们不难发现，<code>pages</code> 对象中的 <code>key</code> 就是入口的别名，而其 <code>value</code> 对象其实是入口 <code>entry</code> 和模板属性的合并，这样我们上述介绍的获取多入口和多模板的方法就可以合并成一个函数来进行多页的处理，合并后的 <code>setPages</code> 方法如下</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// pages 多入口配置</span></span><br><span class="line">exports.setPages = <span class="function"><span class="params">configs</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">let</span> entryFiles = glob.sync(PAGE_PATH + <span class="string">'/*/*.js'</span>)</span><br><span class="line">    <span class="keyword">let</span> map = &#123;&#125;</span><br><span class="line"></span><br><span class="line">    entryFiles.forEach(<span class="function"><span class="params">filePath</span> =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">let</span> filename = filePath.substring(filePath.lastIndexOf(<span class="string">'\/'</span>) + <span class="number">1</span>, filePath.lastIndexOf(<span class="string">'.'</span>))</span><br><span class="line">        <span class="keyword">let</span> tmp = filePath.substring(<span class="number">0</span>, filePath.lastIndexOf(<span class="string">'\/'</span>))</span><br><span class="line"></span><br><span class="line">        <span class="keyword">let</span> conf = &#123;</span><br><span class="line">            <span class="comment">// page 的入口</span></span><br><span class="line">            entry: filePath, </span><br><span class="line">            <span class="comment">// 模板来源</span></span><br><span class="line">            template: tmp + <span class="string">'.html'</span>, </span><br><span class="line">            <span class="comment">// 在 dist/index.html 的输出</span></span><br><span class="line">            filename: filename + <span class="string">'.html'</span>, </span><br><span class="line">            <span class="comment">// 页面模板需要加对应的js脚本，如果不加这行则每个页面都会引入所有的js脚本</span></span><br><span class="line">            chunks: [<span class="string">'manifest'</span>, <span class="string">'vendor'</span>, filename], </span><br><span class="line">            inject: <span class="literal">true</span>,</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (configs) &#123;</span><br><span class="line">            conf = merge(conf, configs)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (process.env.NODE_ENV === <span class="string">'production'</span>) &#123;</span><br><span class="line">            conf = merge(conf, &#123;</span><br><span class="line">                minify: &#123;</span><br><span class="line">                    removeComments: <span class="literal">true</span>, <span class="comment">// 删除 html 中的注释代码</span></span><br><span class="line">                    collapseWhitespace: <span class="literal">true</span>, <span class="comment">// 删除 html 中的空白符</span></span><br><span class="line">                    <span class="comment">// removeAttributeQuotes: true // 删除 html 元素中属性的引号</span></span><br><span class="line">                &#125;,</span><br><span class="line">                chunksSortMode: <span class="string">'manual'</span><span class="comment">// 按 manual 的顺序引入</span></span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        map[filename] = conf</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> map</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>上述代码我们 <code>return</code> 出的 <code>map</code> 对象就是 <code>pages</code> 所需要的配置项结构，我们只需在 <code>vue.config.js</code> 中引用即可</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">/* vue.config.js */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> utils = <span class="built_in">require</span>(<span class="string">'./build/utils'</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">    ...</span><br><span class="line">    </span><br><span class="line">    pages: utils.setPages(),</span><br><span class="line">    </span><br><span class="line">    ...</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>这样我们多页应用基于 <code>pages</code> 配置的改进就大功告成了，当你运行打包命令来查看输出结果的时候，你会发现和之前的方式相比并没有什么变化，这就说明这两种方式都适用于多页的构建，但是这里还是推荐大家使用更便捷的 <code>pages</code> 配置</p>
</blockquote>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2019/06/01/vue-single-page-config/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2019/06/01/vue-router-and-template-analyse/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
